<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_MATRIX_MST.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );

Class_PWD::valid_auth(AOKI_AUTH_MATRIX,$login['staff_auth']);

$dao = new Class_M_MATRIX_MST;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';

	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$forms  = $dao->get( $_GET['id'] );
		$forms["id"] = $_GET['id'];
		//$forms["stop_date"] = Class_PWD::int8_to_date($forms["stop_date"]) ;
		
	}else{
		//$forms['auth'] = 0;
	}
	if( isset($_GET['sort']) && $_GET['sort'] != '' ){
		$forms['sort']     =  $_GET['sort'] ;
		$forms['sortname'] = $aoki_mst_sort[ $_GET['sort'] ] ;
	}else {
		echo "パラメタエラー";
		exit;
	}
}else{
	$forms = $_POST;
	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
		//if( $forms['id'] == '' ) $err_mes['login_pwd']	= $err->check( $forms['login_pwd'], $chk['login_pwd'] );
		/*
		if( $err_mes['name'] == '' ){
			
			if( $forms['id'] == '' ){
				$where = sprintf(" name = '%s'", $forms['name'] );
			}else{
				$where = sprintf(" name = '%s' AND id <> %s", $forms['name'], $forms['id'] );
			}
			if ( $dao->exits($where) ) {
				$err->clear = false;
				$err_mes['cd'] = '<p class="error">このIDは重複しています</p>';
			}
			
		}
		*/
		if ( $err->clear ){
			$cols     = $dao->get_cols();
			foreach($cols as $kcol => $val) {
				$values[$kcol] = $forms[$kcol] ;
			}
			//$values["media_cd"]        =  Class_M_OTHER::do_media($values["media_name"]);
			//$values["stop_date"]       = Class_PWD::date_to_int8 ($values['stop_date']) ;
			//桁数チェック
			$maxlens = $dao->get_maxlens();
			foreach ( $maxlens as $k => $v ) {
				$err_mes[$k] =$err->check_size($values[$k], $v);
			}
			if( $err_mes['cd'] == '' ){
				if( $forms['id'] == '' ){
					$where = sprintf(" cd = '%s' AND sort = '%s' ", $forms['cd'], $forms['sort']  );
				}else{
					$where = sprintf(" cd = '%s' AND id <> '%s' AND sort = '%s'  ", $forms['cd'], $forms['id'], $forms['sort']  );
				}
				if ( $dao->exits($where) ) {
					$err->clear = false;
					$err_mes['cd'] = '<p class="error">このIDは重複しています</p>';
				}
			}
		}
		if( $err->clear ){
			if( $forms['id'] == '' ){
				if ($dao->add0($values)) {
					$smarty->assign('message', '登録が完了しました。');
					
				} else {
					$smarty->assign('message', '登録が失敗しました。');
				}
			}else{
				
				if ($dao->edit( $forms['id'], $values )) {
					$smarty->assign('message', '更新が完了しました。');
				} else {
					$smarty->assign('message', '更新が失敗しました。');
				}
				$id = $forms['cd'];
			}
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}

if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);

if ($phase != 'complete') {
	
}

//$smarty->assign('sorts', $aoki_mst_sort);
$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('mmatrix_master_form.html');
?>